package bite;

/**
 * Creared with IntelliJ IDEA.
 * Description:【查找两个字符串a,b中的最长公共子串】
 * 查找两个字符串a,b中的最长公共子串。若有多个，输出在较短串中最先出现的那个。
 * 注：子串的定义：将一个字符串删去前缀和后缀（也可以不删）形成的字符串。请和“子序列”的概念分开！
 * User:yxd
 * Date:2022-05-01
 * Time:16:32
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Day19_2{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        String sub = input.next();
        if(str.length() < sub.length()){
            System.out.println(getMax(str,sub));
        }else{
            System.out.println(getMax(sub,str));
        }

    }
    //假设str1位最短串
    public static String getMax(String str1,String str2){
        char[] arr1 = str1.toCharArray();
        char[] arr2 = str2.toCharArray();
        //状态定义F(i,j)以i结尾和以j结尾的子串的最长公共子串
        //状态转移方程F(i,j) 如果第i-1个字符和第j-1个字符相等那么就等于F(i - 1,j - 1) + 1不相等就为0
        //状态赋初值F(0,0) = 0第一行全为0,第一列全为0
        int len1 = arr1.length;
        int len2 = arr2.length;
        //起始位置
        int start = 0;
        //最长的子串长度
        int maxLen = 0;
        //会有一辅助列(i,0)和辅助行(0,j)
        int[][] dp = new int[len1 + 1][len2 + 1];
        for (int i = 1; i < dp.length; i++) {
            for (int j = 1; j < dp[0].length; j++) {
                if(arr1[i - 1] == arr2[j - 1]){
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    if(maxLen < dp[i][j]){
                        maxLen = dp[i][j];
                        start = i - maxLen;
                    }
                }else{
                    dp[i][j] = 0;
                }
            }
        }
        if(maxLen == 0){//没有相同的子串
            return "-1";
        }
        //start为最大子串的起始位置
        return str1.substring(start,start + maxLen);
    }




    public static void main1(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        String sub = input.next();
        List<String> list = new ArrayList<>();
        int count = 0;
         //从第一个字符串的第一个字符开始遍历(第二个字符串中如果和这个字符相当就开始寻找是否是最长的)
        for (int i = 0; i < str.length(); i++) {
            for(int j = 0;j < sub.length();j ++){
                char ch1 = str.charAt(i);//开始的字符串
                int x = i;
                if(sub.charAt(j) == ch1){
                    int y = x;
                    int z = j;
                    while(z < sub.length() && x < str.length() && sub.charAt(z) == str.charAt(x)){
                        z ++;
                        x ++;
                    }//相等的字符串
                    if(count > x - y){
                        continue;
                    }else if(count == x - y){
                        list.add(str.substring(y,x));
                    }else{
                        count = x - y;
                        list.clear();
                        list.add(str.substring(y,x));
                    }
                }
            }
        }
        if(str.length() < sub.length()) {
            for (int i = 0; i < str.length(); i++) {
                for (int j = i; j <= str.length(); j++) {
                    if (list.contains(str.substring(i, j))) {
                        System.out.println(str.substring(i, j));

                        return;
                    }
                }
            }
        }else{
            for (int i = 0; i < sub.length(); i++) {
                for (int j = i; j <= sub.length(); j++) {
                    if (list.contains(sub.substring(i, j))) {
                        System.out.println(sub.substring(i, j));
                        return;
                    }
                }
            }
        }
    }


}
